package com.controllers;

import com.config.exception.MyException;
import com.models.RoleMappingPermission;
import com.models.User;
import com.models.UserMappingRole;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/5/9.
 */
public class BaseController {



    public void loginAsUser(HttpServletRequest request, User user) {
//        HttpSession httpSession = request.getSession(true);
//        httpSession.setAttribute("currentUser", user);
//
//        HashSet<String> permissionStrSet = new HashSet<String>();
//        List<UserMappingRole> mappingRoleList = this.entityFinderService.byEqColumn(UserMappingRole.class, "user", user);
//        for (UserMappingRole mappingRole : mappingRoleList) {
//            List<RoleMappingPermission> mappingPermissionList = this.entityFinderService.byEqColumn(RoleMappingPermission.class, "role", mappingRole.getRole());
//            for (RoleMappingPermission mappingPermission : mappingPermissionList) {
//                permissionStrSet.add(mappingPermission.getPermission().getPermission());
//            }
//        }
//
//        httpSession.setAttribute("currentUserPermission", permissionStrSet);
    }

    public void clearLoginedUser(HttpServletRequest request) {
        HttpSession httpSession = request.getSession(true);
        httpSession.removeAttribute("currentUser");
    }

    public User currentUser(HttpServletRequest request) {
        HttpSession httpSession = request.getSession(true);
        User user = (User) httpSession.getAttribute("currentUser");
        return user;
    }

    public HashSet<String> getCurrentUserPermissionStr(HttpServletRequest request) {
        HttpSession httpSession = request.getSession(true);
        return (HashSet<String>) httpSession.getAttribute("currentUserPermission");
    }

    public void checkPermission(HttpServletRequest request, String... permissionStrList) throws MyException {
        HttpSession httpSession = request.getSession(true);
        HashSet<String> permissionStrSet = (HashSet<String>) httpSession.getAttribute("currentUserPermission");
        Boolean tag = false;
        for (String permissionStr : permissionStrList) {
            if (permissionStrSet.contains(permissionStr)) {
                tag = true;
            }
        }
        if (!tag) {
            throw new MyException("您没有此权限！");
        }
    }

    public void checkFileDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
    }

    public String dateToString(Date date, String formatter) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        return sdf.format(date);
    }

    public Date stringToDate(String date, String formatter) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        return sdf.parse(date);
    }


    public String getFullUrlPrefix(HttpServletRequest request) {
        String protocol = "HTTP/1.1".equalsIgnoreCase(request.getProtocol()) ? "http" : "https";
        String portStr;
        int port = request.getServerPort();
        if(protocol.equalsIgnoreCase("http") && port==80) {
            portStr = "";
        } else if(protocol.equalsIgnoreCase("https") && port==443) {
            portStr = "";
        } else {
            portStr = ":" + port;
        }
        return String.format("%s://%s%s", protocol, request.getServerName(), portStr);
    }

}
